package com.mtw.bbs.article.controller.admin;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mtw.bbs.article.pojo.entity.ArticleIndex;
import com.mtw.bbs.article.pojo.form.ArticleForm;
import com.mtw.bbs.article.pojo.vo.ArticleVo;
import com.mtw.bbs.article.pojo.vo.SimpleArticleVo;
import com.mtw.bbs.article.service.ArticleFileService;
import com.mtw.bbs.article.service.ArticleIndexService;
import com.mtw.bbs.common.core.constant.SysConstant;
import com.mtw.bbs.common.core.vo.Result;
import com.mtw.bbs.userAdmin.feign.UserClient;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;



/**
 * 文章管理接口层
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("admin/article")
public class AdminArticleController {

    private final UserClient userClient;
    private final ArticleIndexService articleIndexService;
    private final ArticleFileService articleFileService;




    /**
     * 分页查询文章
     *
     * @param page          当前页
     * @param size          分页大小
     * @param level         推荐等级
     * @param content       内容
     * @param topicId       话题id
     * @param original      是否原创
     * @param articleType   文章类型
     * @param approveStatus 审核状态
     */
    @GetMapping("/page")
    public Result getAdminDtoPage(@RequestParam(name = "page") int page,
                                  @RequestParam(name = "size") int size,
                                  @RequestParam(name = "level", required = false) Integer level,
                                  @RequestParam(name = "content", required = false) String content,
                                  @RequestParam(name = "topicId", required = false) String topicId,
                                  @RequestParam(name = "status", required = false) Integer status,
                                  @RequestParam(name = "original", required = false) Integer original,
                                  @RequestParam(name = "articleType", required = false) Integer articleType,
                                  @RequestParam(name = "approveStatus", required = false) Integer approveStatus) {

        LambdaQueryWrapper<ArticleIndex> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(content), ArticleIndex::getContent, content);
        lqw.eq(level != null, ArticleIndex::getLevel, level);
        lqw.eq(StringUtils.isNotBlank(topicId), ArticleIndex::getTopicId, topicId);
        lqw.eq(original != null, ArticleIndex::getOriginal, original);
        lqw.eq(articleType != null, ArticleIndex::getArticleType, articleType);
        lqw.eq(status != null, ArticleIndex::getStatus, status);
        lqw.eq(approveStatus != null, ArticleIndex::getApproveStatus, approveStatus);
        lqw.orderByDesc(ArticleIndex::getCreatedDate);

        Page<ArticleVo> voPage = articleIndexService.getArticlePage(page, size, lqw);

        return Result.success(voPage);
    }


    /**
     * 根据id获取简单文章
     *
     * @param ids id集合
     */
    @PostMapping("/simpleArticle")
    public Result getSimpleArticle(@RequestBody String[] ids) {

        if (ids == null || ids.length < 1) {
            return Result.success(new ArrayList<>());
        }
        List<SimpleArticleVo> list = articleIndexService.getSimpleArticle(Arrays.stream(ids).collect(Collectors.toList()));
        return Result.success(list);
    }


    /**
     * 保存
     */
    @PostMapping("/save")
    public Result save(@RequestBody ArticleForm form) {
        String saved = articleIndexService.saveArticle(form);
        return saved == null ? Result.fail("保存失败") : Result.success();

    }


    /**
     * 更新文章
     */
    @PutMapping("/update")
    public Result update(@RequestBody ArticleForm form) {


        boolean updated = articleIndexService.updateArticle(form);
        return updated ? Result.success() : Result.fail("更新失败");
    }


    /**
     * 启用文章
     */
    @PutMapping("/enable")
    public Result enableArticle(@RequestBody String[] ids) {

        if (ids == null || ids.length < 1) {
            return Result.fail("更新失败");
        }
        List<String> list = Arrays.asList(ids);
        LambdaUpdateWrapper<ArticleIndex> luw = new LambdaUpdateWrapper<>();
        luw.set(ArticleIndex::getStatus, SysConstant.DEFAULT_ENABLE);
        luw.in(ArticleIndex::getId, list);
        boolean updated = articleIndexService.updateArticleByIds(luw, list);
        return updated ? Result.success() : Result.fail("更新失败");
    }


    /**
     * 禁用文章
     */
    @PutMapping("/disable")
    public Result disableArticle(@RequestBody String[] ids) {

        if (ids == null || ids.length < 1) {
            return Result.fail("更新失败");
        }
        List<String> list = Arrays.asList(ids);
        LambdaUpdateWrapper<ArticleIndex> luw = new LambdaUpdateWrapper<>();
        luw.set(ArticleIndex::getStatus, SysConstant.DEFAULT_DISABLE);
        luw.in(ArticleIndex::getId, list);
        boolean updated = articleIndexService.updateArticleByIds(luw, list);
        return updated ? Result.success() : Result.fail("更新失败");
    }


    /**
     * 删除文章
     *
     * @param id 文章id
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteArticle(@PathVariable String id) {

        boolean result = articleIndexService.deleteArticle(Collections.singletonList(id));
        return result ? Result.success() : Result.fail("删除失败");
    }


    /**
     * 删除文章
     *
     * @param ids 文章id数组
     */
    @DeleteMapping("/delete")
    public Result deleteArticles(@RequestBody String[] ids) {

        boolean result = articleIndexService.deleteArticle(Arrays.asList(ids));
        return result ? Result.success() : Result.fail("删除失败");
    }

}
